﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using System.Windows;

namespace Filmek
{
    public partial class Form1 : Form
    {
        private bool Err;
        private int jog;
		private int felhID;
		private String felhNev;
        private MyReader myReader;
		//private object prevSelected = null;
		//private CheckedListBox.CheckedItemCollection prevSelected = null;
		//private int prevSelected = -1;
		private String prevSelected = null;
		public RadioButton[] lekerdezesekRadio = null;
		private String searchByTxt = "Film";
        private Kosar k;


        public Form1(int id, int j)
        {
            myReader = new MyReader(ref Err);
            myReader.xmlExport();
            jog = j;
			felhID = id;
			k = new Kosar(felhID);
            InitializeComponent();
            hideButtons();
        }

		//az Uj gomb nyomasa
        private void UjBut_Click(object sender, EventArgs e)
        {
			ModositFilm myModositFilmForm = null;

			String searchBy = searchByCombo.Text;
			//String item = mySearch.Text;

			//String item2 = listView1.FocusedItem.Text;
			String item2 = null;

			//valaszt attol fugg mi volt kivalasztva fent
			switch (searchBy)
			{
				case "Film":
					myModositFilmForm = new ModositFilm(null,"Film");
					myModositFilmForm.Show();
					break;

				case "Szinesz":
					myModositFilmForm = new ModositFilm(null,"Szinesz");
					myModositFilmForm.Show();
					break;

				case "Rendezo":
					myModositFilmForm = new ModositFilm(null, "Rendezo");
					myModositFilmForm.Show();
				break;
			}
        }

		//Modosit gomb nyomasa
        private void ModositBut_Click(object sender, EventArgs e)
        {
            String searchBy = searchByCombo.Text;
            String item = mySearch.Text;
			String item2 = null;
			String itemTypeToModify = null;

			ModositFilm myModositFilmForm;

			if (listView1.FocusedItem != null)
			{
				item2 = listView1.FocusedItem.Text;
				itemTypeToModify = searchByCombo.Text;
			}
            if (jog != 2)//ha user akkor megtekinti(3parameteres meghivas)
            {
                //valasztas
                switch (searchBy)
                {
                    case "Film":
                        myModositFilmForm = new ModositFilm(item2, itemTypeToModify);
                        myModositFilmForm.Show();
                        break;

                    case "Szinesz":
                        myModositFilmForm = new ModositFilm(item2, itemTypeToModify);
                        myModositFilmForm.Show();
                        break;

                    case "Rendezo":
                        myModositFilmForm = new ModositFilm(item2, itemTypeToModify);
                        myModositFilmForm.Show();
                        break;
                }
            }
            else
            {
                //valasztas
                switch (searchBy)
                {
                    case "Film":
                        myModositFilmForm = new ModositFilm(item2, itemTypeToModify,false);
                        myModositFilmForm.Show();
                        break;

                    case "Szinesz":
                        myModositFilmForm = new ModositFilm(item2, itemTypeToModify,false);
                        myModositFilmForm.Show();
                        break;

                    case "Rendezo":
                        myModositFilmForm = new ModositFilm(item2, itemTypeToModify,false);
                        myModositFilmForm.Show();
                        break;
                }

            }
        }

		//Torles gomb
        private void TorolBut_Click(object sender, EventArgs e)
        {
			String item = null;

			if (listView1.FocusedItem != null)
			{
				item = listView1.FocusedItem.Text;

				myReader.torol(item, searchByCombo.Text);

				mySearch.ResetText();

				myReader.feltoltListView(mySearch, searchByCombo, listView1);
			}
			else
				MessageBox.Show("Valasszon ki valamit amit torolni szeretne!");





        }

		//beallitja a hatterkepet
		private void loadBgImage()
		{
			Image image1 = new Bitmap(@"E:\Dropbox\Egyetem\II ev\I felev\Adatbazis\bgImage22.jpg", true);
			//image1.Size = this.Size;
			this.BackgroundImage = image1;
		}

		//Load
        private void Form1_Load(object sender, EventArgs e)
        {
             myReader = new MyReader(ref Err);
			 //loadBgImage();
            searchByCombo.DataSource = new String[]{"Film","Szinesz","Rendezo"};
        }

        private void searchByCombo_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (searchByCombo.SelectedIndex == 0)
                hozzaadButton.Show();
            else
                hozzaadButton.Hide();
            searchByTxt = searchByCombo.Text;			
			myReader.feltoltListView(mySearch, searchByCombo, listView1);
        }

        private void mouseDoubleClick(object sender, MouseEventArgs e)
        {
            //MessageBox.Show("duplaclick");

            //myReader.getInfo(mySearch, searchByCombo, listView1);
        }

		//amikor beir vmit
        private void mySearch_TextChanged(object sender, EventArgs e)
        {
            myReader.feltoltListView(mySearch, searchByCombo, listView1);
        }

		//amikor megjelennek a lekerdezesek eltunnek a gombok
		private void disableButtons(bool enabled)
		{
			UjBut.Enabled = enabled;
			ModositBut.Enabled = enabled;
			TorolBut.Enabled = enabled;
			searchByCombo.Enabled = enabled;
			mySearch.Enabled = enabled;
		}

		//megjeleniti / eltunteti a lekerdezeseket
		private void lekerdezesekVisible(bool visible)
		{
			LekerdezesekBox.Visible = visible;
			LekerdezesBut.Visible = visible;
			LekerdezesLabel.Visible = visible;
			LekerdezesTxt.Visible = visible;

			disableButtons(!visible);

			if (visible == true)
			{
				myReader.feltoltLekerdezesek(LekerdezesekBox, searchByCombo.Text);
			}
		}

		//emgvaltozik a checkbox
		private void LekerdezesedCheck_CheckedChanged(object sender, EventArgs e)
		{

			if (LekerdezesedCheck.Checked == true)
			{
				this.Size = new Size(650, 600);
				lekerdezesekVisible(true);
			}
			else
			{
				this.Size = new Size(650, 400);
				lekerdezesekVisible(false);

				searchByTxt = searchByCombo.Text;
				myReader.feltoltListView(mySearch, searchByCombo, listView1);
			}
		}

		//figyeli h csak egy leekrdezest lehessen kivalasztani
		private void LekerdezesekBox_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (prevSelected == null)
			{
				prevSelected = (LekerdezesekBox.CheckedItems[0].ToString());
			}
			else
			{
				if (LekerdezesekBox.CheckedItems.Count > 1)
				{
					LekerdezesekBox.SetItemChecked(LekerdezesekBox.Items.IndexOf(prevSelected), false);
					prevSelected = LekerdezesekBox.CheckedItems[0].ToString();
				}
			}
		}

		//lekerdez gomb
		private void LekerdezesBut_Click(object sender, EventArgs e)
		{
			if (LekerdezesekBox.CheckedItems.Count != 0)
				myReader.taroltSelectFeltolt(listView1, LekerdezesekBox, LekerdezesTxt.Text, searchByTxt);
			else
				MessageBox.Show("Valasszon ki egy lekerdezest!");
		}

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }

        private void hideButtons()
        {
            switch (jog)
            {
                case 2: //user
                    {
                        UjBut.Hide();
                        TorolBut.Hide();
                        ModositBut.Text = "Megtekint";
                    } break;
                case 3: //moderator
                    {
                        kosarButton.Hide();
                        hozzaadButton.Hide();
                    } break;


            }

        }

        private void hozzaadButton_Click(object sender, EventArgs e)
        {


            if (listView1.FocusedItem != null)
            {
                string cim = listView1.FocusedItem.Text;
                int itemID = myReader.getFilmIDByName(cim);

                if (myReader.getFilmDbByName(cim) > 0)
                {
                    k.addKosar(itemID);
                    //myReader.kiveszRaktar(cim);
					//nem kell kivenni a raktarbol csak miutan megvasarolja..
                }
                else
                    MessageBox.Show("nincs tobb a raktaron");

                   
            }
            else
                MessageBox.Show("nincs kivalasztva semmi");
        }

        private void kosarButton_Click(object sender, EventArgs e)
        {
            KosarForm f = new KosarForm(k);
            f.Show();
        }
    }
}
